package org.noka.select;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;

import org.nokatag.password.AESPlus;
import org.nokatag.system.BugInfoWrint;
import org.nokatag.system.Concest;
import org.nokatag.system.Function;
import org.nokatag.tagjava.NokaTag;
/**
 * 下拉选择框
 * @author rebin
 *
 */
public class Select extends  NokaTag{

	private static final long serialVersionUID = 7550610672397733696L;
	private  JspWriter jspOut =null;
	
	private String id = null;
	private String name = null;
	private String width = "120";
	private String sql = null;//sql方式加载
	private String json = null;//json方式加载 [{id:sss,f},{value:dd}]
	private String height = "100";//下拉框高度
	private String onchange=null;//值改变调用外部js
	private String value = null;
	private String readonly=null;//只读
	private String disabled = null;
	private String allownull="true";
	private String placeholder = null;
	private String lower = null;//下级选择框的id
	private String slang = null;
	private String selectwidth=null;//弹出选框的宽度
	private String optionwidth=null;//选项宽度
	private String findsql = null;//输入查询sql
	private String dataurl = null;//数据加载url
	private String findurl = null;//查找url
	
	
	public int doStartTag()throws JspException{
		try{
			jspOut= pageContext.getOut();
			HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
			String basePath = basepath(request);
			if(null==sql && null!=dataurl){
				sql = "test";
			}
			if(null==findsql && null!=findurl){
				findsql = "test";
			}
			StringBuilder body = new StringBuilder();
			body.append(potjs(request));//js框架
			body.append(selectCss(request));
			body.append(selectjs(request));//select js
			String hei=LenNumber(height);
			FormHtmlInsertid(getId(),getName(),getValue());
			body.append("<div id=\""+id+"_sDiv\">");
			body.append("<div id=\""+id+"_selectDiv\" style=\"float:left;padding:0px;margin:0px;\"></div>");
			body.append("<div id=\""+id+"_sboddiv\"  style=\"overflow:auto;z-index: 999;background-color: #fff;display:none;position:absolute;overflow-x:hidden;height:"+hei+"px;top:0px;left:0px;border: 1px solid #ddd;\"></div>");
			body.append("</div>");
			body.append("<script type=\"text/javascript\">");
			//---------------显示树形菜单---------------------------
			body.append("Event.observe(document, 'dom:loaded', function() {");
			body.append("var sfg"+id+" = {");
			body.append("id:'"+id+"',");//id
			body.append("rurl:'"+basePath+"',");//根url
			if(null!=selectwidth){
				body.append("selectwidth:'"+LenFormat(selectwidth)+"',");//弹出框的宽度
			}
			if(null!=optionwidth){
				body.append("optionwidth:'"+LenFormat(optionwidth)+"',");//弹出框的宽度
			}
			body.append("slang:'"+(slang!=null?slang:Function.lung(request,"org_nokatag_select_pleaseselect"))+"',");//语言
			body.append("name:'"+(name==null?id:name)+"',");//名字
			body.append("lower:'"+(lower==null?"":lower)+"',");
			body.append("json:"+(json==null?"''":json.replace("'", "\""))+",");//初始始值
			body.append("height:"+hei+",");//宽
			body.append("value:'"+(value==null?"":value)+"',");//值
			body.append("readonly:'"+(readonly==null?"":readonly)+"',");//只读
			body.append("allownull:"+allownull+",");
			body.append("placeholder:'"+(placeholder==null?"":placeholder)+"',");
			body.append("onchange:function(node){"+(onchange==null?"return true":"return "+onchange+"(node);")+"},");//onchange
			body.append("width:"+LenNumber(width)+",");//宽
			body.append("disabled:'"+(disabled==null?"":disabled)+"',");
			if(null!=findsql){
				body.append("findsql:'"+(findsql==null?"":AESPlus.encrypt(findsql, Concest.SQLKEY))+"',");//find sql
			}
			if(null!=dataurl){
				body.append("dataurl:'"+dataurl+"',");
			}
			if(null!=findurl){
				body.append("findurl:'"+findurl+"',");
			}
			body.append("sql:'"+(sql==null?"":AESPlus.encrypt(sql, Concest.SQLKEY))+"'");//sql
			body.append("};");
			body.append("var nr"+id+" = new nselect(sfg"+id+");");
			body.append("nr"+id+".show();");
			body.append("});");
			body.append("</script>");
			jspOut.write(body.toString());
		}catch(Exception se){
			 BugInfoWrint.Print(se);
		}
		return EVAL_BODY_INCLUDE;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getWidth() {
		return width;
	}

	public void setWidth(String width) {
		this.width = width;
	}

	public String getSql() {
		return sql;
	}

	public void setSql(String sql) {
		this.sql = sql;
	}

	public String getJson() {
		return json;
	}

	public void setJson(String json) {
		this.json = json;
	}

	public String getHeight() {
		return height;
	}

	public void setHeight(String height) {
		this.height = height;
	}

	public String getOnchange() {
		return onchange;
	}

	public void setOnchange(String onchange) {
		this.onchange = onchange;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public String getReadonly() {
		return readonly;
	}

	public void setReadonly(String readonly) {
		this.readonly = readonly;
	}

	public String getAllownull() {
		return allownull;
	}

	public void setAllownull(String allownull) {
		this.allownull = allownull;
	}

	public String getPlaceholder() {
		return placeholder;
	}

	public void setPlaceholder(String placeholder) {
		this.placeholder = placeholder;
	}

	public String getLower() {
		return lower;
	}

	public void setLower(String lower) {
		this.lower = lower;
	}

	public String getSlang() {
		return slang;
	}

	public void setSlang(String slang) {
		this.slang = slang;
	}

	public String getSelectwidth() {
		return selectwidth;
	}

	public void setSelectwidth(String selectwidth) {
		this.selectwidth = selectwidth;
	}

	public String getOptionwidth() {
		return optionwidth;
	}

	public void setOptionwidth(String optionwidth) {
		this.optionwidth = optionwidth;
	}

	public String getFindsql() {
		return findsql;
	}

	public void setFindsql(String findsql) {
		this.findsql = findsql;
	}

	public String getDisabled() {
		return disabled;
	}

	public void setDisabled(String disabled) {
		this.disabled = disabled;
	}

	public String getDataurl() {
		return dataurl;
	}

	public void setDataurl(String dataurl) {
		this.dataurl = dataurl;
	}

	public String getFindurl() {
		return findurl;
	}

	public void setFindurl(String findurl) {
		this.findurl = findurl;
	}
}
